/*********************************************************************
																			   
	 TITLE: School randomization 
	 PROJECT: Teaching and Learning in Multi-grade Rural Schools: Experimental 
	 Evidence from Colombia											   
																			   
	DESCRIPTION: This dofile conducts balance checks of the school 
	randomization
	
 
*********************************************************************/

/*********************************************************************
								0. Setup		
*********************************************************************/

clear all

*------------------------------------
* 0.1. Set Directories
*------------------------------------

global gen_dir
		
cd "${gen_dir}"
	
*------------------------------------
* 0.2. Set paths
*------------------------------------
	
global data_use
global tables_balance 

*------------------------------------
* 0.3. Other settings
*------------------------------------
	
version 13.1
set more off
set seed 141650963

/*********************************************************************
						1. CHECK BALANCE	
*********************************************************************/

*------------------------------------
* 1.1. Call data
*------------------------------------

use "${data_use}/schools_randomized.dta", clear 

*------------------------------------
* 1.2 Check balance
*------------------------------------
	
* *	* NO WIEGHTS * * *
	
	 global vars_balance_sede porc_estrato01 porc_hombre prom_edad_total porc_zona porc_fea tmp_bogota_sede sede_ppia  ninos_sede  docentes_aula 
	
	loc r = `: word count $vars_balance_sede'
	local rr=(`r'*2)

	matrix coefi = J(`rr', 4, .)
	matrix colnames coefi = "Mean" "OLS coeff. (se)" "p-value" "Sample size" 

	local i=1
	local j=2

	foreach x in $vars_balance_sede {
		qui areg `x' tratamiento, absorb(bloque_distancia) r 
		
		mat coefi[`i',2]= round(_b[tratamiento], 0.001)
		mat coefi[`j',2]= round(_se[tratamiento], 0.0001)
		mat coefi[`i',3]= round((2 * ttail(e(df_r), abs(_b[tratamiento]/_se[tratamiento]))), 0.001)
		mat coefi[`i',4]= e(N)
		sum  `x' if e(sample)==1  & tratamiento==0
		mat coefi[`i',1]= round(r(mean), 0.001)
		mat coefi[`j',1]= round(r(sd), 0.001)
		
		local i=`i'+ 2
		local j=`j'+ 2
		local lab_`x': variable label `x'
	}

	mat list coefi  

preserve 
	
	drop _all 
	svmat coefi

	rename (coefi1 coefi2 coefi3 coefi4) (mean beta pvalue obs)

	gen variable=""
	 
	local i=1
	local j=2 
	 
	 forvalue x=1/`r' {
		local this: word `x' of $vars_balance_sede
		replace variable="`lab_`this''" if _n==`i'
		local i=`i'+2
	 }
	
	gen stars_beta = "*" if pvalue<=0.1 & pvalue>0.05 & pvalue!=.
	replace stars_beta = "**" if pvalue<=0.05 & pvalue>0.01 & pvalue!=.
	replace stars_beta = "***" if pvalue<=0.01 & pvalue!=.
	
	foreach var of varlist mean beta pvalue  {
		
		gen `var'_s=string(`var',"%9.2f")
		replace `var'_s="" if `var'_s=="."
		replace `var'_s="0"+`var'_s if substr(`var'_s,1,1)=="." 
		drop `var'
		rename `var'_s `var'
		
		}
	
	gen coeff = beta + stars_beta
	replace coeff="("+coeff+")" if variable==""
	replace mean="("+mean+")" if variable==""

	keep variable mean coeff pvalue obs
	order variable mean coeff pvalue obs

	label var variable "Variable"
	label var mean "Control mean (s.d.)"
	label var coeff "OLS coeff. (s.e.)"
	label var pvalue "P-value"
	label var obs "Obs."

	
	#delimit ;
	global note1 "The values are averages at the school level. Low socioeconomic status means strata 0 and 1 (strata takes the value between 0 and 6). The average age from primary students includes all students in grades equal o less than 5. 'CCT' stands for conditional cash transfer, and in this setting, refers to the program 'Familias en Acción'. Significance levels are indicated by $*$ $<.1$, ** $<.05$,  *** $<.01$. Robust standard errors are presented below the regression coefficient. Distance blocks fixed effects are included.";
	#delimit cr
	texsave using "${tables_balance}/balance_rand.tex", width(\textwidth) ///
	replace size(scriptsize) align(lCCCC) location(htbp) varlabels frag ///
	title(Balance at the time of randomization) marker(tab:balance-rand) ///
	footnote("$note1", size(scriptsize)) nofix
restore 

* *	* NUMBER OF STUDENTS WIEGHTS * * *

	 global vars_balance_sede porc_estrato01 porc_hombre prom_edad_total porc_zona porc_fea tmp_bogota_sede sede_ppia  ninos_sede  docentes_aula 
	
	loc r = `: word count $vars_balance_sede'
	local rr=(`r'*2)

	matrix coefi = J(`rr', 4, .)
	matrix colnames coefi = "Mean" "OLS coeff. (se)" "p-value" "Sample size" 

	local i=1
	local j=2

	foreach x in $vars_balance_sede {
		qui areg `x' tratamiento [aw=ninos_sede],  absorb(bloque_distancia) r 
		
		mat coefi[`i',2]= round(_b[tratamiento], 0.001)
		mat coefi[`j',2]= round(_se[tratamiento], 0.0001)
		mat coefi[`i',3]= round((2 * ttail(e(df_r), abs(_b[tratamiento]/_se[tratamiento]))), 0.001)
		mat coefi[`i',4]= e(N)
		sum  `x' if e(sample)==1  & tratamiento==0
		mat coefi[`i',1]= round(r(mean), 0.001)
		mat coefi[`j',1]= round(r(sd), 0.001)
		
		local i=`i'+ 2
		local j=`j'+ 2
		local lab_`x': variable label `x'
	}

	mat list coefi  

preserve 
	
	drop _all 
	svmat coefi

	rename (coefi1 coefi2 coefi3 coefi4) (mean beta pvalue obs)

	gen variable=""
	 
	local i=1
	local j=2 
	 
	 forvalue x=1/`r' {
		local this: word `x' of $vars_balance_sede
		replace variable="`lab_`this''" if _n==`i'
		local i=`i'+2
	 }
	
	gen stars_beta = "*" if pvalue<=0.1 & pvalue>0.05 & pvalue!=.
	replace stars_beta = "**" if pvalue<=0.05 & pvalue>0.01 & pvalue!=.
	replace stars_beta = "***" if pvalue<=0.01 & pvalue!=.
	
	foreach var of varlist mean beta pvalue  {
		
		gen `var'_s=string(`var',"%9.2f")
		replace `var'_s="" if `var'_s=="."
		replace `var'_s="0"+`var'_s if substr(`var'_s,1,1)=="." 
		drop `var'
		rename `var'_s `var'
		
		}
	
	gen coeff = beta + stars_beta
	replace coeff="("+coeff+")" if variable==""
	replace mean="("+mean+")" if variable==""

	keep variable mean coeff pvalue obs
	order variable mean coeff pvalue obs

	label var variable "Variable"
	label var mean "Control mean (s.d.)"
	label var coeff "OLS coeff. (s.e.)"
	label var pvalue "P-value"
	label var obs "Obs."

	
	#delimit ;
	global note1 "The values are averages at the school level. Low socioeconomic status means strata 0 and 1 (strata takes the value between 0 and 6). The average age from primary students includes all students in grades equal o less than 5. 'CCT' stands for conditional cash transfer, and in this setting, refers to the program 'Familias en Acción'. Analytic weights by the number of students in each school are added. Significance levels are indicated by $*$ $<.1$, ** $<.05$,  *** $<.01$. Robust standard errors are presented below the regression coefficient. Distance blocks fixed effects are included.";
	#delimit cr
	texsave using "${tables_balance}/balance_rand_weights.tex", width(\textwidth) ///
	replace size(scriptsize) align(lCCCC) location(htbp) varlabels frag ///
	title(Balance at the time of randomization (weighted by student enrollment)) marker(tab:balance-rand-weights) ///
	footnote("$note1", size(scriptsize)) nofix
restore 
	
